package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionOperator {
    public List<Integer> getListByInterval(int left, int right) {
        //选出给定区间中所有的数字
        Integer[] listByInterval = new Integer[]{};
        List<Integer> resultListByInterval = Arrays.asList(listByInterval);
        if (left<right){
            for(int i=left+1;i<right; i++){
                resultListByInterval.add(i);
            }
        }else {
            for(int i=right+1;i<left; i++){
                resultListByInterval.add(i);
            }
        }
        return resultListByInterval;

        //throw new NotImplementedException();
    }

    public List<Integer> getEvenListByIntervals(int left, int right) {
        //选出给定区间中所有的偶数
        Integer[] EvenListByInterval = new Integer[]{};
        List<Integer> resultEvenListByInterval = Arrays.asList(EvenListByInterval);
        if (left<right){
            for(int i=left;i<right; i++){
                if (i%2==0){
                    resultEvenListByInterval.add(i);
                }

            }
        }else {
            for(int i=right+1;i<left; i++){
                if (i%2==0){
                    resultEvenListByInterval.add(i);
                }
            }
        }
        return resultEvenListByInterval;
     //   throw new NotImplementedException();
    }

    public List<Integer> popEvenElments(int[] array) {
        //选出给定区间中所有的偶数
        Integer[] evenList = new Integer[]{};
        List<Integer> popEvenList = Arrays.asList(evenList);
        for (int i=0;i<array.length;i++){
            if (array[i]%2==0){
                popEvenList.add(i);
            }
        }
        return popEvenList;
       // throw new NotImplementedException();
    }

    public int popLastElment(int[] array) {
        //弹出集合最后一个元素
        return array[array.length - 1];

       // throw new NotImplementedException();
    }

    public List<Integer> popCommonElement(int[] firstArray, int[] secondArray) {
        //弹出两个集合的交集
        List<Integer> result = new ArrayList<>();
        for (int num : firstArray) {
            if (isContainedIntArray(num, secondArray)) {
                result.add(num);
            }
        }
        return result;

       // throw new NotImplementedException();
    }
    private boolean isContainedIntArray(int num, int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int anArr : arr) {
            list.add(anArr);
        }
        return list.contains(num);
    }

    public List<Integer> addUncommonElement(Integer[] firstArray, Integer[] secondArray) {
        // 将集合二中与集合一不同的元素加入集合一
        List<Integer> result = new ArrayList<>(Arrays.asList(firstArray));
        for (int num : secondArray) {
            if (!isContainedIntegerArray(num, firstArray)) {
                result.add(num);
            }
        }
        return result;
    }

    private boolean isContainedIntegerArray(int num, Integer[] firstArray) {
        List<Integer> list = new ArrayList<>();
        for (int anArr : firstArray) {
            list.add(anArr);
        }
        return list.contains(num);
    }
}

